<!DOCTYPE html>
<html>
<head>
    {include file="_Fragment/meta" /}
    <link rel="stylesheet" href="/static/pear/component/pear/css/pear.css" />
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" lay-filter="reload">
            <div class="layui-form-item">
                <div class="layui-form-item layui-inline">
                    <div class="layui-input-inline">
                        <input type="text" name="keys" placeholder="课程名称|ID" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item layui-inline">
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="user-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="user-table" lay-filter="user-table"></table>
    </div>
</div>
<script type="text/html" id="user-bar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="view"><i class="layui-icon layui-icon-search"></i></button>
</script>
<script type="text/html" id="create-time">
    {{layui.util.toDateString(d.update_time*1000, 'yyyy-MM-dd HH:mm:ss')}}
</script>
{include file="_Fragment/javascript" /}
<script>
    layui.use(['table', 'form', 'jquery'], function() {
        let table = layui.table;
        let form = layui.form;

        let $ = layui.jquery;

        let cols = [
            [{
                    title: 'ID',
                    field: 'id',
                    align: 'center',
                    fixed: 'left',
                },
                {
                    title: '课程名称',
                    field: 'title',
                    align: 'center',
                    fixed: 'left',
                },
                {
                    title: '学习人数',
                    field: 'total',
                    align: 'center',
                    fixed: 'left',
                    sort: true
                },
                {
                    title: '完成人数',
                    field: 'complete_total',
                    align: 'center',
                },
                {
                    title: '学习时长(H)',
                    field: 'duration',
                    align: 'center',
                    sort: true
                },
                {
                    title: '播放量',
                    field: 'play_total',
                    align: 'center',
                    sort: true
                },
                {
                    title: '分享量',
                    field: 'share_total',
                    align: 'center',
                    sort: true
                },
                {
                    title: '发帖',
                    field: 'topic_total',
                    align: 'center',
                    sort: true
                },
                {
                    title: '评论',
                    field: 'comment_total',
                    align: 'center',
                    sort: true
                },
                {
                    title: '更新时间',
                    field: 'update_time',
                    align: 'center',
                    templet: '#create-time'
                },
                {
                    title: '操作',
                    toolbar: '#user-bar',
                    align: 'center',
                }
            ]
        ]

        table.render({
            elem: '#user-table',
            url: '/LearningContent/load',
            page: {layout:['count', 'prev', 'page', 'next', 'skip']},
            limit: 10,
            cols: cols,
            //skin: 'line',
            toolbar: '#user-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter',{
                title: '导出' //标题
                ,layEvent: 'export' //事件名，用于 toolbar 事件中使用
                ,icon: 'layui-icon-export' //图标类名
            }]
        });
        form.on('submit(user-query)', function(data) {
            table.reload('user-table', {
                where: data.field
                ,page: {
                    curr: 1, //重新从第 1 页开始
                    layout:['count', 'prev', 'page', 'next', 'skip']
                }
                ,limit: 10,
            })
            return false;
        });

        table.on('tool(user-table)', function(obj) {
            if (obj.event === 'view') {
                window.view(obj);
            }
        });


        //监听排序事件
        table.on('sort(user-table)', function(obj){ //注：sort 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var field = form.val('reload');
            table.reload('user-table', {
                initSort: obj //记录初始排序，如果不设的话，将无法标记表头的排序状态。
                ,where: { //请求参数（注意：这里面的参数可任意定义，并非下面固定的格式）
                    field: obj.field //排序字段
                    ,order: obj.type //排序方式
                    ,keys: field.keys
                }
            });
        });


        table.on('toolbar(user-table)', function(obj) {
            if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'export') {
                window.export();
            }
        });

        window.view = function(obj) {
            parent.layui.admin.addTab('00'+obj.data.id, obj.data.title+"详情", '/LearningContent/user?learning_id='+obj.data.id)
        }

        // 数据导出
        window.export = function() {
            var field = form.val('reload');
            layer.confirm('确定要导出员工数据吗?', {
                icon: 3,
                title: '提示'
            }, function(index) {
                layer.close(index);
                window.open("{:url('export')}?keys="+field.keys, "_blank");
            });
        };


        window.refresh = function(param) {
            table.reload('user-table');
        }
    })
</script>
</body>
</html>
